package code.google.com.analyticsbot;

import java.util.logging.Logger;
import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import code.google.com.analyticsbot.data.WaveData;
import com.google.wave.api.RobotMessageBundle;

public class WaveDataProvider
{
	private static final PersistenceManagerFactory PMF = JDOHelper.getPersistenceManagerFactory("transactions-optional");
	private static final Logger log = Logger.getLogger(AnalyticsbotServlet.class.getName());

	private PersistenceManager m_pm;
	private WaveData m_data;

	public WaveDataProvider(RobotMessageBundle events)
	{
		this(events.getWavelet().getWaveId(), events.getWavelet().getWaveletId(), true);
	}

	public WaveDataProvider(String waveId, String waveletId, boolean create)
	{
		m_pm = PMF.getPersistenceManager();

		try
		{
			m_data = m_pm.getObjectById(WaveData.class, WaveData.makeKeyString(waveId, waveletId));
			//log.warning("Loaded WaveData version " + m_data.getVersion() + " for waveId=" + waveId + ", waveletId=" + waveletId);
			logData("load");
			m_data.incrementVersion();
		}
		catch(javax.jdo.JDOObjectNotFoundException ex)
		{
			if(create)
			{
				log.warning("Created a new WaveData for waveId=" + waveId + ", waveletId=" + waveletId);
				m_data = new WaveData(waveId, waveletId);
				m_pm.makePersistent(m_data);
			}
			else
			{
				throw new RuntimeException("WaveData not found for waveId=" + waveId + ", waveletId=" + waveletId);
			}
		}
	}

	public WaveData getData()
	{
		return m_data;
	}

	private void logData(String message)
	{
		log.warning(m_data.toString(message));
	}

	public void close()
	{
//		TextView textView = m_blip.getDocument();
//
//		Date now = new Date();
//		StringBuffer newString = new StringBuffer("Analyticsbot report " + now + ":");
//
//
//		int activeUsers = 0;
//		UserData mostActive = null;
//
//		Collection<UserData> users = m_data.getUsersData();
//		for(UserData user : users)
//		{
//
//			newString.append("\n");
//			newString.append("User "+user.getID()+" Created " + user.getCreated()+" blips and modified "+user.getModified());
//			int[] userActivityByHours = user.getActivityByHours();
//			for (int i=0; i< userActivityByHours.length; i++){
//				if (userActivityByHours[i] != 0){
//					newString.append("\tSubmitted " + userActivityByHours[i] + " Blips at hour " + i);
//				}
//			}
//			if (user.isActive()){
//				activeUsers++;
//			}
//			if (user.moreActive(mostActive)){
//				mostActive = user;
//			}
//		}
//
//		newString.append("\n");
//		newString.append("Most Active user is " + mostActive.getID());
//		newString.append("\n");
//		int activeUsersPercentage = (activeUsers*100)/users.size();
//		newString.append("Percentage of Active Users: " + activeUsersPercentage + "% " + activeUsers + " out of " + users.size());
//
//		log.warning(newString.toString());
//		//textView.replace(newString.toString());
//		textView.delete();
//		textView.append(newString.toString());

		logData("save");
		Helpers.makeSerializedDirty(m_data);
		m_pm.close();
	}
}
